package com.yl.immodule.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.android.yl.ylicpsdk.ICPRtc;
import com.android.yl.ylicpsdk.ICPRtcManager;
import com.camera.base.BaseApp;
import com.camera.base.util.YLIMConfig;
import com.camera.routemodule.routeinterface.RouterUtil;
import com.yl.immodule.IMAppManager;
import com.yl.immodule.R;

public class CustomDialog extends Dialog {

    private EditText ipAddressEditText;
    private EditText port1EditText;
    private EditText port2EditText;

    public CustomDialog(Context context) {
        super(context);
        setTitle("设置网络配置");
        setContentView(R.layout.dialog_setip); // 使用自定义布局
        setupView();
    }

    private void setupView() {
        ipAddressEditText = findViewById(R.id.ip_address_edit_text);
        port1EditText = findViewById(R.id.port1_edit_text);
        port2EditText = findViewById(R.id.port2_edit_text);
        ipAddressEditText.setText(YLIMConfig.ip);
        port1EditText.setText(YLIMConfig.sipProt);
        port2EditText.setText(YLIMConfig.httpProt);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置对话框背景为白色
        getWindow().setBackgroundDrawableResource(R.color.white);

        // 设置对话框大小和位置
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        getWindow().setAttributes(params);

        // 添加按钮并设置点击事件
        Button okButton = findViewById(R.id.ok_button);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入的 IP 地址和端口号
                String ipAddress = ipAddressEditText.getText().toString();
                String port1 = port1EditText.getText().toString();
                String port2 = port2EditText.getText().toString();

                YLIMConfig.setIp(ipAddress);
                YLIMConfig.setSipProt(port1);
                YLIMConfig.setHttpProt(port2);
                RouterUtil.INSTANCE.getIMManagerImpl().reInitEngine();
//                ICPRtcManager.reInitFactory((ICPRtc.Configure) RouterUtil.INSTANCE.getIMManagerImpl().getConfig());


                Log.e("TAG", "onClick: "+ ICPRtcManager.getsInstance().getConfig().ip +
                        " - "+ICPRtcManager.getsInstance().getConfig().port+
                        " - "+ICPRtcManager.getsInstance().getConfig().httpPort);
                // 关闭对话框
                dismiss();
            }
        });

        Button cancelButton = findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}